package org.micro.frame.mybatis.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.micro.frame.mybatis.configuration.BaseDataSourceConfig;
import org.micro.frame.mybatis.configuration.MultipleDataSource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MultipleDataSource("test1")
public class Test1DataSourceConfig extends BaseDataSourceConfig {

    @Autowired
    @Qualifier("test1DataSource")
    private DataSource dataSource;

    @Autowired
    @Qualifier("test1SqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

    @Primary
    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.multiple-datasource.test1")
    public DataSource dataSource() {
        return super.buildDataSource();
    }

    @Bean(name = "test1SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() {
        return super.buildSqlSessionFactory(dataSource);
    }

    @Bean(name = "test1DataSourceTransactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager() {
        return super.buildDataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate() {
        return super.buildSqlSessionTemplate(sqlSessionFactory);
    }

}
